﻿/*
 * ========================================================================
 * Copyright(c) 2007-2012 情留メ蚊子, All Rights Reserved.
 * Welcom use the LiteFetion.
 * See more information,Please goto http://www.94qing.com
 * ========================================================================
 * 
 * 作 者：情留メ蚊子
 * Q  Q: 540644769
 * 邮 箱：qlwz@qq.com
 * 网 址：http://www.94qing.com
 * ========================================================================
*/
using System;
using LiteFetion.Utils;

namespace LiteFetion.Model
{
    /// <summary>
    /// 飞信好友
    /// </summary>
    public class Buddy
    {
        public Buddy()
        {
            this.SMSPolicy = new SMSPolicy();
        }

        /// <summary>
        /// 用户ID，唯一的标识符
        /// </summary>
        public int UserId { get; set; }

        /// <summary>
        /// Uri
        /// </summary>
        private string _uri;
        /// <summary>
        /// URI，没有实际的含义，也可以标识一个用户
        /// </summary>
        public string Uri
        {
            get { return _uri; }
            set
            {
                this._uri = value.Trim();
                if (_uri.StartsWith("sip"))
                {
                    this.Sid = Util.ParseInt(_uri.Substring(4, (_uri.IndexOf('@') == -1 ? _uri.Length - 1 : _uri.IndexOf('@'))));
                }
                else if (_uri.StartsWith("tel"))
                {
                    this.Mobile = _uri.Substring(4);
                }
            }
        }

        /// <summary>
        /// 飞信号，仅在开通飞信的时候有效
        /// </summary>
        public int Sid { get; set; }

        /// <summary>
        /// 分组编号，可以有多个分组编号，用；隔开
        /// </summary>
        public string CordIds { get; set; }

        /// <summary>
        /// 用户设置的好友的备注
        /// </summary>
        public string LocalName { get; set; }

        /// <summary>
        /// 昵称
        /// </summary>
        public string NickName { get; set; }

        /// <summary>
        /// 是否在黑名单中
        /// </summary>
        public bool IsBlack { get; set; }

        /// <summary>
        /// 该好友和用户的关系
        /// </summary>
        public Relation Relation { get; set; }

        /// <summary>
        /// 手机号，权限关系，不一定有效
        /// </summary>
        public string Mobile { get; set; }

        /// <summary>
        /// 心情短语
        /// </summary>
        public string Impresa { get; set; }

        /// <summary>
        /// 头像的crc校验码
        /// </summary>
        public string Crc { get; set; }

        /// <summary>
        /// 定义为在某段时间内不把飞信发送到手机上
        /// </summary>
        public SMSPolicy SMSPolicy { get; private set; }

        public BuddyState _buddyState;
        /// <summary>
        /// 获取状态
        /// </summary>
        /// <returns></returns>
        public BuddyState BuddyState
        {
            get
            {
                BuddyState state = BuddyState.OFFLINE;
                if (this.Relation == Relation.BUDDY)
                {
                    state = this._buddyState;
                    if (state == BuddyState.OFFLINE)
                    {
                        state = this.SMSPolicy.IsSMSOnline ? BuddyState.SMS : BuddyState.OFFLINE;
                    }
                }
                else
                {	//非好友，只能是离线状态
                    state = BuddyState.OFFLINE;
                }
                return state;
            }
            set
            {
                _buddyState = value;
            }
        }

        /// <summary>
        /// 获取可以显示的名字
        /// </summary>
        /// <returns></returns>
        public string DisplayName
        {
            get
            {
                if (!this.LocalName.IsEmpty())
                {
                    return this.LocalName;
                }
                if (!this.NickName.IsEmpty())
                {
                    return this.NickName;
                }
                if (this.Sid > 0)
                {
                    return this.Sid.ToString();
                }
                if (!this.Mobile.IsEmpty())
                {
                    return this.Mobile;
                }
                return this.UserId.ToString();
            }
        }

        public override string ToString()
        {
            return "Buddy [UserId=" + this.UserId + ", Uri=" + this.Uri + ", Sid=" + this.Sid + ", DisplayName="
                    + this.DisplayName + ", Relation=" + this.Relation + ", BuddyState=" + this.BuddyState + "]";
        }

        /// <summary>
        /// 取得好友关系的枚举
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Relation GetRelation(int value)
        {
            switch (value)
            {
                case 0: return Relation.UNCONFIRMED;
                case 1: return Relation.BUDDY;
                case 2: return Relation.DECLINED;
                default: return Relation.STRANGER;
            }
        }

        /// <summary>
        /// 取得好友状态的枚举
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static BuddyState GetBuddyState(int value)
        {
            switch (value)
            {
                case 0: return BuddyState.OFFLINE;
                case 400: return BuddyState.ONLINE;
                case 600: return BuddyState.BUSY;
                case 100: return BuddyState.AWAY;
                case 499: return BuddyState.ROBOT;
                case 1: return BuddyState.SMS;
                default: return BuddyState.OFFLINE;
            }
        }
    }
}
